package com.pg.agent.engine.service.utils;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSONObject;
import com.pg.agent.engine.client.model.ComponentInParam;
import com.pg.agent.engine.client.model.InParam;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 类名
 *
 * @author huhuaping
 * @date 2024/4/1 18:02
 */
public class ParamUtil {

    public void resolver(List<ComponentInParam> componentInParamList, Map<String,String> headers, Map<String,Object> queryParam, Map<String,Object> pathParam, Map<String,Object> bodyParam){

        if(componentInParamList!=null){

            componentInParamList.forEach(componentInParam -> {

                if (ObjectUtil.equals(componentInParam.getParamPosition(),1)) {
                    headers.put(componentInParam.getParamCode(),componentInParam.getParamCode());
                }else if (ObjectUtil.equals(componentInParam.getParamPosition(),1)) {

                }
            });
        }

    };
    public static Integer getInteger(List<InParam> inParamList, String paramCode){

        if( inParamList == null ){
            return null;
        }
        InParam first = inParamList.stream().filter(a -> a.getParamCode().equals(paramCode)).findFirst().get();
        if(first != null && first.getParamValue()!=null){
           return Integer.valueOf(first.getParamValue().toString());
        }
        return null;
    };
    public static String getString(List<InParam> inParamList, String paramCode){
        if( inParamList == null ){
            return null;
        }
        InParam first = inParamList.stream().filter(a -> a.getParamCode().equals(paramCode)).findFirst().get();
        if(first != null && first.getParamValue()!=null){
           return first.getParamValue().toString();
        }
        return null;
    };

    public static Object getObject(List<InParam> inParamList, String paramCode){
        if( inParamList == null ){
            return null;
        }
        InParam first = inParamList.stream().filter(a -> a.getParamCode().equals(paramCode)).findFirst().get();
        if(first != null && first.getParamValue()!=null){
            return first.getParamValue();
        }
        return null;
    }


    public static void putParam(List<InParam> inParamList, JSONObject userParam){
        if( inParamList == null ){
            inParamList = new ArrayList<>();
        }
        if( userParam == null ){
            return;
        }
        for (String key:userParam.keySet()){
            if(!inParamList.contains(key)){
                inParamList.add(new InParam(key,userParam.get(key)));
            }
        }
    }
    public static void putParam(List<InParam> inParamList,  Map<String, JSONObject> userInput){
        if( inParamList == null ){
            inParamList = new ArrayList<>();
        }
        if( userInput == null ){
            return;
        }
        for (String key:userInput.keySet()){
            if(!inParamList.contains(key)){
                inParamList.add(new InParam(key,userInput.get(key)));
            }
        }
    }
}
